﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using Microsoft.Win32;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Reflection;
using System.Diagnostics;
using System.Xml;
using System.IO;

namespace delge_squirreleditor
{
    public class ProjectItem : TreeViewItem
    {
        public string Name { get; set; }
        public string Path { get; set; }
        public string FileName { get { return Name + ".nut"; } }
        public string FilePath { get { return Path + "\\" + FileName; } }

        public bool IsEditing { get; protected set; }

        public ScriptEditor Editor { get; set; }

        private EditableTextBlock textboxEditName;

        public delegate void RenamedEventHandler(object sender, EventArgs e);
        public delegate void RemoveEventHandler(object sender, EventArgs e);

        public event RenamedEventHandler Renamed;
        public event RemoveEventHandler Remove;

        public ProjectItem(string name, string path)
        {
            Name = name;
            Path = path;

            IsEnabled = File.Exists(FilePath);
            
            textboxEditName = new EditableTextBlock();
            textboxEditName.Text = Name;
            textboxEditName.ImageSource = new BitmapImage(new Uri("Images/script_file.png", UriKind.Relative));
            if (!IsEnabled)
                textboxEditName.Text += "* Could not find file";
            else
                Editor = new ScriptEditor(this.FilePath);
            textboxEditName.FinishedEditing += new EditableTextBlock.FinishedEditingEventHandler(textboxEditName_FinishedEditing);

            Header = textboxEditName;
            ToolTip = FilePath;

            ContextMenu = new ContextMenu();
            MenuItem renameItem = new MenuItem() { Header = "Rename", InputGestureText = "F2" };
            MenuItem removeItem = new MenuItem() { Header = "Remove", InputGestureText = "Del" };
            renameItem.Click += new RoutedEventHandler(renameItem_Click);
            removeItem.Click += new RoutedEventHandler(removeItem_Click);
            ContextMenu.Items.Add(renameItem);
            ContextMenu.Items.Add(removeItem);
        }

        public void Update()
        {
            IsEnabled = File.Exists(FilePath);
            textboxEditName.Text = Name;
            if (!IsEnabled)
                textboxEditName.Text += "* Could not find file";
            else
                Editor = new ScriptEditor(this.FilePath);
        }

        public void renameItem_Click(object sender, RoutedEventArgs e)
        {
            BeginEdit();
        }

        public void removeItem_Click(object sender, RoutedEventArgs e)
        {
            OnRemove(EventArgs.Empty);
        }

        public void BeginEdit()
        {
            (Header as EditableTextBlock).IsEditing = true;
            IsEditing = true;
        }

        public void OpenEditor(AvalonDock.DockingManager manager)
        {
            Editor.Show(manager);
            Editor.Activate();
        }

        protected virtual void OnRemove(EventArgs e)
        {
            if (Remove != null) Remove(this, e);
        }

        protected virtual void OnRenamed(EventArgs e)
        {
            ToolTip = FilePath;
            if (Editor != null) Editor.Rename(FilePath);
            if (Renamed != null) Renamed(this, e);
        }

        protected void textboxEditName_FinishedEditing(object sender, EventArgs e)
        {
            this.Name = textboxEditName.Text;
            OnRenamed(EventArgs.Empty);
        }
    }
}
